package cn.jwcat.service.impl;

import cn.jwcat.entity.Ipaddress;
import cn.jwcat.entity.Login;
import cn.jwcat.entity.Staff;
import cn.jwcat.entity.User;
import cn.jwcat.mapper.IpaddressMapper;
import cn.jwcat.mapper.StaffMapper;
import cn.jwcat.service.IOtherService;
import cn.jwcat.service.IStaffService;
import cn.jwcat.service.IUserService;
import cn.jwcat.utils.IPUtils;
import cn.jwcat.utils.JwtTokenUtil;
import cn.jwcat.utils.MD5;
import cn.jwcat.utils.otherUtils;
import cn.jwcat.utils.result.Result;
import cn.jwcat.utils.result.ResultCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Transactional
@Service
public class OtherServiceImpl implements IOtherService {
	@Resource
	StaffMapper staffMapper;

	@Resource
	JwtTokenUtil jwtTokenUtil;

	@Resource
	IpaddressMapper ipaddressMapper;

	@Resource
	IPUtils ipUtils;

	@Resource
	IStaffService staffService;

	@Resource
	IUserService userService;

	@Override
	public Result login(Login login) {
		Staff staff = staffMapper.selectOne(Wrappers.lambdaQuery(Staff.class)
			.eq(Staff::getUsername, login.getUsername())
			.eq(Staff::getStatus, true));
		if (null == staff) return Result.api(ResultCodeEnum.USER_LOGIN_ERROR);
		if (!MD5.getTrue(login.getPassword(), staff.getPassword()))
			return Result.api(ResultCodeEnum.USER_LOGIN_ERROR_PASSWORD);
		Map<String, String> token = new HashMap<>();
		String jwt = jwtTokenUtil.createToken(staff.getStaffId());
		token.put("token", jwt);
		token.put("tokenHeader", jwtTokenUtil.getHeader());
		return Result.api(ResultCodeEnum.USER_LOGIN_SUCCESS, token);
	}

	@Override
	public Result getInfo(HttpServletRequest request) {
		Staff staff = jwtTokenUtil.getStaffFromRequest(request);
		String ip = ipUtils.getIpAddr(request);
		Ipaddress ipAddress = ipUtils.delRegionToIpAddress(ip);
		int i = ipaddressMapper.postIpAddress(ipAddress);
		if (i != 1) return Result.api(ResultCodeEnum.JWT_Malformed_ERROR);
		Staff delStaff = otherUtils.delStaff(staff);
		delStaff.setBool(staffService.get(delStaff.getStaffId()));
		return Result.success(delStaff);
	}

	@Override
	public Result userLogin(Login login) {
		LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
		wrapper
			.eq(User::getUname, login.getUsername())
			.eq(User::getStatus, true);
		User user = userService.getOne(wrapper);
		if (null == user) {
			return Result.api(ResultCodeEnum.USER_LOGIN_ERROR);
		}
		if (!MD5.getTrue(login.getPassword(), user.getPassword())) {
			return Result.api(ResultCodeEnum.USER_LOGIN_ERROR_PASSWORD);
		}
		Map<String, String> token = new HashMap<>();
		String jwt = jwtTokenUtil.createToken(user.getUserId());
		token.put("token", jwt);
		token.put("tokenHeader", jwtTokenUtil.getHeader());
		return Result.api(ResultCodeEnum.USER_LOGIN_SUCCESS, token);
	}
}
